package com.marktim.pokerpoker.menu;

import com.marktim.pokerpoker.R;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListPopupWindow;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public class MenuChipsAdapter implements SpinnerAdapter {

	private int[][] data;

	private Context ctx;

	public MenuChipsAdapter(Context ctx, int[][] data) {
		this.ctx = ctx;
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.length;
	}

	@Override
	public Object getItem(int position) {
		return data[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null)
			convertView = LayoutInflater.from(ctx).inflate(R.layout.menu_create_spinner_row, null);

		TextView[] values = new TextView[] {
				(TextView) convertView.findViewById(R.id.whiteCount),
				(TextView) convertView.findViewById(R.id.redCount),
				(TextView) convertView.findViewById(R.id.greenCount),
				(TextView) convertView.findViewById(R.id.blueCount),
				(TextView) convertView.findViewById(R.id.blackCount),
		};

		for (int i = 0; i < values.length && i < data[position].length; i++) {
			values[i].setText(Integer.toString(data[position][i]));
		}

		return convertView;
	}

	@Override
	public int getViewTypeCount() {
		return 1;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isEmpty() {
		return false;
	}

	@Override
	public void registerDataSetObserver(DataSetObserver observer) {
	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {
	}

	@Override
	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		if (convertView == null)
			convertView = LayoutInflater.from(ctx).inflate(R.layout.menu_create_spinner_row, null);

		TextView[] values = new TextView[] {
				(TextView) convertView.findViewById(R.id.whiteCount),
				(TextView) convertView.findViewById(R.id.redCount),
				(TextView) convertView.findViewById(R.id.greenCount),
				(TextView) convertView.findViewById(R.id.blueCount),
				(TextView) convertView.findViewById(R.id.blackCount),
		};

		for (int i = 0; i < values.length && i < data[position].length; i++) {
			values[i].setText(Integer.toString(data[position][i]));
		}

		convertView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, ctx.getResources().getDimensionPixelSize(
				R.dimen.actionbar_margin)));

		return convertView;
	}

}
